package com.press.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.press.services.InitService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller("initController")
@RequestMapping
public class InitController {
    @Resource(name = "initService")
    private InitService initService;
    private static Logger log = Logger.getLogger(InitController.class);
    public InitService getInitService() {
        return initService;
    }

    public void setInitService(InitService initService) {
        this.initService = initService;
    }

    public void init(HttpServletRequest request, HttpServletResponse response){
        ServletContext application = request.getServletContext();
        init(application);
    }
    public void init(ServletContext application){
        log.info("配置信息Option开始初始化！");
        Map<String,String > option = initService.init();
        application.setAttribute("Option",option);
        application.setAttribute("CTPATH",application.getContextPath()+"/"+option.get("web_template_id"));
        application.setAttribute("CONTEXTPATH",application.getContextPath());
        application.setAttribute("URLIndex",application.getContextPath()+"/");
        application.setAttribute("URLContent",application.getContextPath()+"/content");
        application.setAttribute("URLSearch",application.getContextPath()+"/search");
        application.setAttribute("URLTaxonomy",application.getContextPath()+"/taxonomy");
        application.setAttribute("URLUser",application.getContextPath()+"/user");
        application.setAttribute("isInitOption","true");
        log.info("配置信息Option初始化完成!");
    }
    public void initJson(ServletContext application){
        InputStream input = application.getResourceAsStream("/WEB-INF/admin/content/ueditor/jsp/config.json");
        try {
            String config = IOUtils.toString(input,"utf-8");
            JSONObject json = JSON.parseObject(config);
            json.put("imageUrlPrefix",application.getContextPath());
            String filename = application.getRealPath("/WEB-INF/admin/content/ueditor/jsp/config.json");
            FileUtils.copyInputStreamToFile(IOUtils.toInputStream(json.toJSONString(),"utf-8"),new File(filename));
            log.info(json.get("imageUrlPrefix"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
